package com.example.yian.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yian.config.OssConfig;
import com.example.yian.controller.SecurityRiskController;
import com.example.yian.entity.SecurityRiskReport;
import com.example.yian.mapper.SecurityRiskReportMapper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class SecurityRiskReportService {
    @Autowired
    private OssConfig ossConfig;

    @Autowired
    private SecurityRiskReportMapper SecurityRiskReportMapper;

    public String getSecurityRiskReportUrl(XWPFDocument document) throws IOException {
        //上传到oos中
        String fileName = UUID.randomUUID().toString();
        OSS ossClient = new OSSClientBuilder().build(
                ossConfig.getEndpoint(),
                ossConfig.getAccessKeyId(),
                ossConfig.getAccessKeySecret()
        );

        try {
            ossClient.putObject(new PutObjectRequest(ossConfig.getBucketName(), fileName, document.getDocument().newInputStream()));
            String url = "https://" + ossConfig.getBucketName() + "." + ossConfig.getEndpoint() + "/" + fileName;

            int i =0;
        }
        catch (Exception e)
        {
        }
        finally {
            ossClient.shutdown();
            document.close();
        }
        return "";
    }

    public void addReport(String url, Long userId, String reportId) {
        // 插入到数据库
        LocalDateTime now = LocalDateTime.now();
        SecurityRiskReport riskReport = new SecurityRiskReport();
        riskReport.setOos_url(url);
        riskReport.setUserId(userId);
        riskReport.setReportId(reportId);
        riskReport.setCreatedAt(Timestamp.valueOf(now));
        SecurityRiskReportMapper.insert(riskReport);
    }

    /**
     * 根据user_id分页获取隐患列表
     * @param userId 用户ID
     * @param page 当前页码
     * @param size 每页数量
     * @return 分页后的隐患列表数据
     */
   /* public IPage<SecurityRiskReport> getRiskReportListsByUserId(Long userId, int page, int size, String startTime, String endTime) {
       int offset = (page - 1) * size;
        Page<SecurityRiskReport> pageInfo = new Page<>(page, size);
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        List<SecurityRiskReport> riskReports = SecurityRiskReportMapper.getRiskReportListByUserIdWithPagination(pageInfo, userId, startTime, endTime, size, offset);
        pageInfo.setRecords(riskReports);

        return pageInfo;
    }*/

    public IPage<SecurityRiskReport> getRiskReportListsByUserId(Long userId, int page, int size, String startTime, String endTime, boolean isExportAll) {
        // 处理时间格式（仅当 isExportAll 为 false 时使用）
        if (!isExportAll) {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
        }

        if (isExportAll) {
            // 如果 isExportAll 为 true，则不进行分页，获取最近 30 条记录，忽略 startTime 和 endTime
            List<SecurityRiskReport> riskReports = SecurityRiskReportMapper.getRecentRiskReports(userId, 30);
            Page<SecurityRiskReport> pageInfo = new Page<>(page, riskReports.size());
            pageInfo.setRecords(riskReports);
            return pageInfo;
        } else {
            // 否则，按照分页参数获取数据，并使用 startTime 和 endTime 进行过滤
            int offset = (page - 1) * size;
            Page<SecurityRiskReport> pageInfo = new Page<>(page, size);
            List<SecurityRiskReport> riskReports = SecurityRiskReportMapper.getRiskReportListByUserIdWithPagination(pageInfo, userId, startTime, endTime, size, offset);
            pageInfo.setRecords(riskReports);
            return pageInfo;
        }
    }

}
